package cn.hnucm.userservice.controller;

import cn.hnucm.userservice.entity.TbLog;
import cn.hnucm.userservice.entity.TbToken;
import cn.hnucm.userservice.entity.TbUserBasis;
import cn.hnucm.userservice.service.TbLogService;
import cn.hnucm.userservice.service.TbTokenService;
import cn.hnucm.userservice.utils.IPUtils;
import cn.hnucm.userservice.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统用户Token 前端控制器
 * </p>
 *
 * @author zjl
 * @since 2021-12-08
 */
@Api(tags = "tb_Token类")
@RestController
@RequestMapping("/userservice/tbToken")
public class TbTokenController {

    @Autowired
    private TbTokenService tbTokenService;
    @Autowired
    private TbLogService tbLogService;
    @Autowired
    private HttpServletRequest req;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 路径： /userservice/tbToken/getToken/1
     * 描述: 根据userId查询tb_token表
     *
     * @param userId 用户id
     * @return List<TbToken>
     */

    @GetMapping("/getToken/{userId}")
    public List<TbToken> getTokenById(@PathVariable("userId") int userId) {
        long startTime = System.currentTimeMillis();
        List<TbToken> tbTokenList = tbTokenService.findTbTokenById(userId);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tbTokenList.size() == 0) {
            userid = 0;
        } else {
            userid = userId;
        }
        TbLog tbLog = new TbLog(userid, "查找用户登录令牌", "getTokenById", "{userId:" + userId + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        tbLogService.addTbLog(tbLog);
        return tbTokenList;
    }

    /**
     * 路径： /userservice/tbToken/addToken
     * 描述: 向tb_token表添加token
     *
     * @param tbToken 令牌
     * @return boolean
     */

    @PostMapping("/addToken")
    public boolean addToken(@RequestBody TbToken tbToken) {
        //System.out.println(tbToken);
        long startTime = System.currentTimeMillis();
        boolean tToken = tbTokenService.addTbToken(tbToken);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tToken == true) {
            userid = tbToken.getUserId();
        } else {
            userid = 0;
        }
        TbLog tbLog = new TbLog(userid, "新增用户登录令牌", "addToken", "{tbToken:" + tbToken + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        tbLogService.addTbLog(tbLog);
        return tToken;
    }

    /**
     * 路径： /userservice/tbToken/updateToken
     * 描述: 根据userId更新tb_token表的数据
     *
     * @param tbToken 令牌
     * @return boolean
     */

    @PostMapping("/updateToken")
    public boolean updateToken(@RequestBody TbToken tbToken) {
        long startTime = System.currentTimeMillis();
        boolean tToken = tbTokenService.updateTbToken(tbToken);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tToken == true) {
            userid = tbToken.getUserId();
        } else {
            userid = 0;
        }
        TbLog tbLog = new TbLog(userid, "更新用户登录令牌", "updateToken", "{tbToken:" + tbToken + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        //System.out.println(tbLog);
        tbLogService.addTbLog(tbLog);
        return tToken;
    }

    /**
     * 路径： /userservice/tbLog/deleteToken
     * 描述: 根据userId删除tb_token表的数据
     *
     * @param userId 用户ID
     * @return boolean
     */

    @PostMapping("/deleteToken")
    public boolean deleteToken(@RequestBody int userId) {
        long startTime = System.currentTimeMillis();
        boolean tToken = tbTokenService.deleteTbToken(userId);
        long endTime = System.currentTimeMillis();
        Date d = new Date(startTime);
        int userid;
        if (tToken == true) {
            userid = userId;
        } else {
            userid = 0;
        }
        TbLog tbLog = new TbLog(Integer.parseInt(req.getHeader("userId")), "删除用户登录令牌", "deleteToken", "{userId:" + userId + "}", endTime - startTime, IPUtils.getIpAddr(req), dateFormat.format(d));
        //System.out.println(tbLog);
        tbLogService.addTbLog(tbLog);
        return tToken;
    }
}

